<?php 
class Edit_page extends MY_Controller {
	function __construct() {
		parent::__construct();
		$this->load->helper('file');
	}
	
	function main() {
		$this->load->view('admin/edit_page/main');
	}
	
	function sfp_setting_form() {
		
		////////////////////////常量设置//////////////////////////
		$data[0]['description'] = "专版视频";
		$data[0]['name'] = "SOCIETY_FESTIVAL_VIDEO";
		$data[0]['value'] = SOCIETY_FESTIVAL_VIDEO;
		
		$piecemaker_file = read_file(PIECEMAKER_CONFIG_FILE);
		
		$society_festival_banner = "/<Image Id=\"society_festival\" Source=\"(.*)\"><Hyperlink URL=\"\/index\.php\/frontend\/society\/society_festival\" \/><\/Image>/";
		preg_match($society_festival_banner, $piecemaker_file, $society_festival_banner_details);
		
		$data[1]['description'] = "站点首页文化节Banner";
		$data[1]['name'] = "SOCIETY_FESTIVAL_BANNER";
		$data[1]['value'] = $society_festival_banner_details[1];
		
		$temp['data'] = $data;
		$this->load->view('admin/edit_page/sfp_setting_form', $temp);
	}
	
	function sfp_setting_submit() {
		
		$constants_file = read_file(CONSTANTS_CONFIG_FILE);
		
		$piecemaker_file = read_file(PIECEMAKER_CONFIG_FILE);
		
		$society_festival_setting_array = $this->input->post("society_festival_setting_array");

		$society_festival_video = "/define\('SOCIETY_FESTIVAL_VIDEO', '(.*?)'\);/";
		$society_festival_banner = "/<Image Id=\"society_festival\" Source=\"(.*)\"><Hyperlink URL=\"\/index\.php\/frontend\/society\/society_festival\" \/><\/Image>/";
		
		preg_match($society_festival_banner, $piecemaker_file, $society_festival_banner_details);
		
		if ($society_festival_setting_array[0] != SOCIETY_FESTIVAL_VIDEO) {
			$constants_file = preg_replace($society_festival_video, "define('SOCIETY_FESTIVAL_VIDEO', '" . $society_festival_setting_array[0] . "');", $constants_file);
		}
		
		if ($society_festival_setting_array[1] != $society_festival_banner_details[1]) {
			$piecemaker_file = preg_replace($society_festival_banner, '<Image Id="society_festival" Source="' . $society_festival_setting_array[1] . '"><Hyperlink URL="/index.php/frontend/society/society_festival" /></Image>', $piecemaker_file);
		}
		
		if (write_file(CONSTANTS_CONFIG_FILE, $constants_file) && write_file(PIECEMAKER_CONFIG_FILE, $piecemaker_file)) {
			$this->output->set_output(json_encode("succeed"));
		} else {
			$this->output->set_output(json_encode("save_failed"));
		}
	}
	
	function sp_setting_form() {
		$piecemaker_file = read_file(PIECEMAKER_CONFIG_FILE);
		
		$static_page_banner = "/<Image Id=\"static_page_(.*)\" Source=\"(.*)\"><Hyperlink URL=\"(.*)\" Target=\"_blank\" \/><\/Image>/";
		preg_match_all($static_page_banner, $piecemaker_file, $static_page_banner_details);
		
		foreach ($static_page_banner_details as $k => $v) {
			foreach ($v as $k1 => $v1) {
				if ($k1 < 9) {
					$page_num = "0" . ($k1 + 1);
				} else {
					$page_num = $k1 + 1;
				}
				
				//index_banner: 0; static_page: 1;
				$data[$k1][0]['description'] = "静态页面" . $page_num . "Banner";
				$data[$k1][0]['name'] = "STATIC_PAGE_" . $page_num . "_BANNER";
				$data[$k1][0]['value'] = $static_page_banner_details[2][$k1];
				
				$data[$k1][1]['description'] = "静态页面" . $page_num . "源文件";
				$data[$k1][1]['name'] = "STATIC_PAGE_" . $page_num . "_SOURCE";
				$data[$k1][1]['value'] = $static_page_banner_details[3][$k1];
			}
		}
		
		$temp['data'] = $data;
		$this->load->view('admin/edit_page/sp_setting_form', $temp);
	}
	
	function sp_setting_submit() {
		$piecemaker_file = read_file(PIECEMAKER_CONFIG_FILE);
		
		$static_page_setting_array = $this->input->post("static_page_setting_array");

		$static_page_banner_1 = "/<Image Id=\"static_page_1\" Source=\"(.*)\"><Hyperlink URL=\"(.*)\" Target=\"_blank\" \/><\/Image>/";
		$static_page_banner_2 = "/<Image Id=\"static_page_2\" Source=\"(.*)\"><Hyperlink URL=\"(.*)\" Target=\"_blank\" \/><\/Image>/";

		$piecemaker_file = preg_replace($static_page_banner_1, '<Image Id="static_page_1" Source="' . $static_page_setting_array[0][0] . '"><Hyperlink URL="' . $static_page_setting_array[0][1] . '" Target="_blank" /></Image>', $piecemaker_file);

		$piecemaker_file = preg_replace($static_page_banner_2, '<Image Id="static_page_2" Source="' . $static_page_setting_array[1][0] . '"><Hyperlink URL="' . $static_page_setting_array[1][1] . '" Target="_blank" /></Image>', $piecemaker_file);
		
		if (write_file(PIECEMAKER_CONFIG_FILE, $piecemaker_file)) {
			$this->output->set_output(json_encode("succeed"));
		} else {
			$this->output->set_output(json_encode("save_failed"));
		}
	}
	
	function set_ckfinder_session() {
		$session = $this->input->post("session");
		
		if ($session == "index_banner") {
			$_SESSION['index_banner'] = 1; //index_banner_session为1时就进入index_banner
			$this->output->set_output(json_encode("succeed"));
		} elseif ($session == "static_page") {
			$_SESSION['static_page'] = 1; //static_page_session为1时就进入static_page
			$this->output->set_output(json_encode("succeed"));
		}
	}
	
}